package com.europe.business.europebusiness.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.util.Log;


import com.europe.business.europebusiness.R;
import com.europe.business.europebusiness.ui.activity.base.ActionBarActivity;
import com.europe.business.europebusiness.ui.bean.User;
import com.europe.business.europebusiness.ui.bean.UserLogin;
import com.europe.business.europebusiness.ui.common.Commons;
import com.europe.business.europebusiness.ui.net.IUser;
import com.europe.business.europebusiness.ui.utils.SpUtils;
import com.europe.business.europebusiness.ui.utils.Utils;


import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class LoginActivity extends ActionBarActivity implements View.OnClickListener {


    private TextView login;
    private EditText username;
    private EditText password;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     showToastShort(getResources().getDisplayMetrics().density+"");


    }

    @Override
    protected void initView() {
        super.initView();
        login = findViewById(R.id.login_tv);
        username=findViewById(R.id.username_msg);
        password=findViewById(R.id.verify_code_msg);

        login.setOnClickListener(this);
        setToolbarNavigationIcon(R.drawable.actionbar_back);
        setActionRight(true, getString(R.string.register), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startAct(SelectIdentityActivity.class);
            }
        });
    }

    @Override
    protected int setMainContent() {
        return R.layout.activity_login;
    }

    @Override
    protected void regClicked() {
        startAct(SelectIdentityActivity.class);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.login_tv:
                String userName=username.getText().toString();
                String passWord=password.getText().toString();
                if (TextUtils.isEmpty(userName)||TextUtils.isEmpty(passWord)){
                    showToastShort("密码或用户名不能为空");
                    return;
                }
                showProgressDialog();
                Retrofit retrofit = new Retrofit.Builder()
                        .addConverterFactory(GsonConverterFactory.create())
                        .baseUrl(Commons.SERVER_URL)
                        .build();
                IUser user=retrofit.create(IUser.class);
                User u=new User();
                u.setName(userName);
                u.setPassword(passWord);
                Call<UserLogin> call=user.login(userName,passWord,Utils.getSystemLanguage(this));
                call.enqueue(new Callback<UserLogin>() {
                    @Override
                    public void onResponse(Call<UserLogin> call, Response<UserLogin> response) {
                        dismissProgressDialog();
                        if(null != response){
                            UserLogin userLogin = response.body();

                            if(userLogin.getErrorCode() == 1){
                                SpUtils.saveToSp(LoginActivity.this, "token", userLogin.getData().getToken());
                                startAct(MainActivity.class);
                                finish();
                            }else{
                                //showToastLong(getString(R.string.login_failure));
                                showToastLong(userLogin.getErrorMessage());
                            }
                            Log.i("Login", response.body()+"");
                        }else{
                            showToastLong("登录成功");
                        }
                    }

                    @Override
                    public void onFailure(Call<UserLogin> call, Throwable t) {
                        dismissProgressDialog();
                        Log.i("Login_err", "失败"+t.getMessage());
                        showToastLong("登录失败");
                    }
                });
                break;
        }
    }
}
